/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.upload.controller;

import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Lists;
import info.peigen.hotpot.component.upload.service.UploadService;
import info.peigen.hotpot.component.upload.service.order.UploadBatchOrder;
import info.peigen.hotpot.component.upload.service.order.UploadOrder;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Param;
import org.noear.solon.core.handle.UploadedFile;

/**
 * <b>(UploadController)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/11/10
 */
@Controller
@Mapping("/upload")
public class UploadController {
    @Inject
    private UploadService uploadService;

    @Mapping("/upload")
    public String upload(@Param("file") UploadedFile file, @Param("path") String path) {
        UploadOrder order = UploadOrder.builder().uploadedFile(file).modulePath(path).build();
        return JSON.toJSONString(uploadService.upload(order));
    }

    @Mapping("/uploads")
    public String uploads(@Param("files") UploadedFile[] files) {
        UploadBatchOrder order = UploadBatchOrder.builder().uploadedFileList(Lists.newArrayList(files)).build();
        return JSON.toJSONString(uploadService.uploadBatch(order));
    }
}